faApp.controller('SimuManagerListCtrl',['$scope','simuManagerService','$location','$controller','$route',function($scope,simuManagerService,$location,$controller,$route) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.list = [];
  $scope.loading = true;

	$scope.$watch('config',function(){
    simuManagerService.list($scope.config).then(function(json){
      $scope.list = json.list;
      $scope.pager = json.pager;
      $scope.loading = false;
    });
  },true)

	$scope.config = {
		"pageSize" : 20,
		"pageNo" : 1
	};

  $scope.searchMap = {
		"gsmc" : '', 
  };

  $scope.search = function(){

    angular.forEach($scope.searchMap,function(o,i){
      $scope.config[i] = ( undefined == $scope.searchMap[i] ? "" : $scope.searchMap[i]);
    });

  };
  
  
	$scope.pageChanged = function(){
		$scope.config.pageNo = $scope.pager.pageNo;
	}; 
  
  
  $scope.remove = function(id){
    simuManagerService.remove({id:id}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $route.reload();
      }
    });
  };
}]);

faApp.controller('SimuManagerAddCtrl',['$scope','simuCompanyService','simuManagerService','simuService','$location','$controller','$route',function($scope,simuCompanyService,simuManagerService,simuService,$location,$controller,$route) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.item = {}; 
  $scope.simuCompanyListing = [];
  $scope.simuCompanyListed = [];
  $scope.companyKw = "";
  
  $scope.$watch('companyKw',function(){
    $scope.companyKw = $scope.companyKw.trim();
    if ($scope.companyKw){
      simuCompanyService.list({pageSize:20,pageNo:1,gsmc:$scope.companyKw}).then(function(json){
        $scope.simuCompanyListing = json.list;
      });
    }else{
      $scope.simuCompanyListing = [];
    }
  });
  
  
  $scope.selectCompany = function(id,idx,isCur){
    angular.forEach($scope.simuCompanyListing,function(o){
      if (o.id == id){
        var existFlag = false;
        angular.forEach($scope.simuCompanyListed,function(oo){
          if (oo.id == id)existFlag = true;
        });
        if (!existFlag) {
          if (isCur){
            $scope.simuCurCompanyListed = [o];
            $scope.item.nowJjgsId = id;
            $scope.item.dqgzdsmjjgs = o.gsmc;
          }else{
            $scope.simuCompanyListed.push(o);
          }
        }
      }
    });
  };
  
  $scope.unSelectCompany = function(id,idx,isCur){
    if (isCur){
      $scope.simuCurCompanyListed = [];
      $scope.item.nowJjgsId = "";
      $scope.item.dqgzdsmjjgs = "";
    }else{
      $scope.simuCompanyListed.splice(idx,1);
    }
  }; 

   $scope.getJJBM=function(){
    simuService.queryjjbm({"type":"JL","digit":"5"}).then(function(json){
       $scope.item.jjjlbm=json.code;
    });
  };

  $scope.getJJBM();
  $scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先正确填写表单");
      return;
    }else{
      if($scope.simuCompanyListed.length){
        $scope.item.beforeJjgsIds = $scope.item.beforeJjgsIds || "";
        var beforeJjgsIdsList = $scope.item.beforeJjgsIds.split(",");
        angular.forEach($scope.simuCompanyListed,function(o){
          beforeJjgsIdsList.push(o.id);
        });
        $scope.item.beforeJjgsIds = beforeJjgsIdsList.join(",").trim(",");
      }
      simuManagerService.add($scope.item).then(function(json){
        if (json.statusCode){
          if(json.statusCode=='2003'){
            alert("经理编码重复");
          }else{
           alert("出错了:" + json.statusCode);
          }
        }else{
          $location.path("/simu/manager/list");
        }
      });
    }
  };
   $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        $scope.item.jlzpdz = response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  }); 
   

}]);

faApp.controller('SimuManagerEditCtrl',['$scope','simuCompanyService','simuManagerService','$location','$controller','$route','$routeParams',function($scope,simuCompanyService,simuManagerService,$location,$controller,$route,$routeParams) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.item = {};
  $scope.loading = true;
  $scope.id = $routeParams.id;
  $scope.simuCompanyListing = [];
  $scope.simuCompanyListed = [];
  $scope.simuCurCompanyListed = [];
  $scope.companyKw = "";
  
  $scope.$watch('companyKw',function(){
    $scope.companyKw = $scope.companyKw.trim();
    if ($scope.companyKw){
      simuCompanyService.list({pageSize:20,pageNo:1,gsmc:$scope.companyKw}).then(function(json){
        $scope.simuCompanyListing = json.list;
      });
    }else{
      $scope.simuCompanyListing = [];
    }
  });
  
  $scope.selectCompany = function(id,idx,isCur){
    angular.forEach($scope.simuCompanyListing,function(o){
      if (o.id == id){
        var existFlag = false;
        angular.forEach($scope.simuCompanyListed,function(oo){
          if (oo.id == id)existFlag = true;
        });
        if (!existFlag) {
          if (isCur){
            $scope.simuCurCompanyListed = [o];
            $scope.item.nowJjgsId = id;
            $scope.item.dqgzdsmjjgs = o.gsmc;
          }else{
            $scope.simuCompanyListed.push(o);
          }
        }
      }
    });
  };
  
  $scope.unSelectCompany = function(id,idx,isCur){
    if (isCur){
      $scope.simuCurCompanyListed = [];
      $scope.item.nowJjgsId = "";
      $scope.item.dqgzdsmjjgs = "";
    }else{
      $scope.simuCompanyListed.splice(idx,1);
    }
  };

  simuManagerService.query({id:$scope.id}).then(function(json){
    $scope.item = json.detail;
    $scope.simuCurCompanyListed = [json.nowCompany];
    $scope.simuCompanyListed = json.beforeCompanyList;
    $scope.loading = false;
  });
  
  $scope.save = function(){
    if ($scope.form.$invalid){
      alert("请先正确填写表单");
      return;
    }else{
      if($scope.simuCompanyListed.length){
        $scope.item.beforeJjgsIds = $scope.item.beforeJjgsIds || "";
        var beforeJjgsIdsList = $scope.item.beforeJjgsIds.split(",");
        angular.forEach($scope.simuCompanyListed,function(o){
          beforeJjgsIdsList.push(o.id);
        });
        $scope.item.beforeJjgsIds = beforeJjgsIdsList.join(",").trim(",");
      }
      simuManagerService.edit($scope.item).then(function(json){
        if (json.statusCode){
          alert("出错了:" + json.statusCode);
        }else{
          $location.path("/simu/manager/list");
        }
      });
    }
  };
   $scope.onUploadFile({
    showMessage : function(m){ alert(m); },
    onComplete:function(f,response){
      if (response.statusCode == "0000") {
        $scope.item.jlzpdz = response.attribute.fileUrl;
        $scope.$apply();
      } else { 
        alert('上传失败'); 
      }
    }    
  }); 
}]);

faApp.controller('SimuManagerViewCtrl',['$scope','$sce','simuManagerService','$location','$controller','$route','$routeParams',function($scope,$sce,simuManagerService,$location,$controller,$route,$routeParams) {   
	$controller('BaseCtrl', {$scope: $scope}); 

  $scope.item = {};
  $scope.nowCompany = {};
  $scope.beforeCompanyList = [];
  $scope.loading = true;
  $scope.id = $routeParams.id;
  
  simuManagerService.query({id:$scope.id}).then(function(json){
    $scope.item = json.detail;
    $scope.beforeCompanyList = json.beforeCompanyList;
    $scope.nowCompany = json.nowCompany;
      //前台html展示
    $scope.item.jjjljs=$sce.trustAsHtml($scope.item.jjjljs);
  });

	$scope.config = {
		"pageSize" : 20,
		"pageNo" : 1
	};
  
	$scope.pageChanged = function(){
		$scope.config.pageNo = $scope.pager.pageNo;
	}; 
  
  $scope.remove = function(id){
    simuManagerService.remove({id:id}).then(function(json){
      if (json.statusCode){
        alert("出错了:" + json.statusCode);
      }else{
        $location.path("/simu/manager/list");
      }
    });
  };
}]);






